package com.moddevapp.phoneutil;

import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.util.Log;


public class PhoneUtilActivity extends Activity {
    /** Called when the activity is first created. */
	class MissedCallsContentObserver extends ContentObserver
	{
	    public MissedCallsContentObserver()
	    {
	        super(null);
	    }

	    @Override
	    public void onChange(boolean selfChange)
	    {
	        Cursor cursor = getApplicationContext().getContentResolver().query(
	            Calls.CONTENT_URI, 
	            null, 
	            Calls.TYPE +  " = ? AND " + Calls.NEW + " = ?", 
	            new String[] { Integer.toString(Calls.MISSED_TYPE), "1" }, 
	            Calls.DATE + " DESC ");

	        //this is the number of missed calls
	        //for your case you may need to track this number
	        //so that you can figure out when it changes
	        cursor.getCount(); 
	        if(cursor.moveToLast()) {
	        	Log.d("PhoneUtil", "Colum: " + cursor.getColumnCount());
	        	int columCount = cursor.getColumnCount();
	        	for(int i = 0; i < columCount; i++) {
	        		Log.d("PhoneUtil", "Colum Name: " + cursor.getColumnName(i));
	        	}
	        	Log.d("PhoneUtil", "Number: " + cursor.getString(6));
	        	Log.d("PhoneUtil", "Data: " + cursor.getString(8));
	        }     
	    }
	}	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        MissedCallsContentObserver mcco = new MissedCallsContentObserver();
        getApplicationContext().getContentResolver().registerContentObserver(Calls.CONTENT_URI, true, mcco);
    }
}